package com.spps;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtils {
    // 数据库连接地址
    private String url = "";

    // 数据库账号
    private String user = "";

    // 数据库密码
    private String password = "";

    // 构造方法
    public JDBCUtils(String url, String user, String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    // 获取数据库控制器
    public Statement getStatement() throws SQLException, ClassNotFoundException {
        // 载入数据库驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 创建数据库连接
        Connection connection = DriverManager.getConnection(url, user, password);
        // 创建数据库控制器
        return connection.createStatement();
    }

    // 执行SQL语句
    public ResultSet executeQuery(String sql) throws ClassNotFoundException, SQLException {
        return this.getStatement().executeQuery(sql);
    }
}
